//
//  HomeViewController.m
//  YearsProgram
//
//  Created by 王理朝 on 2019/5/5.
//  Copyright © 2019 王理朝. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeCell.h"
#import "AddFestivalEventController.h"
#import "MaskLabel.h"
#import "WaveView.h"

@interface HomeViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *sourcyAry;

@property (strong, nonatomic) WaveView *waveView;

@property (strong, nonatomic) MaskLabel *maskLabel;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self makeView];
    [self makeHeadView];
    [self dataList];
}



- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - TABBAR_HEIGHT - TOPBAR_HEIGHT) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedRowHeight = 200;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (void)addEvent {
    for (int i = 0; i < 10; i ++ ) {
        NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
        NSString *month = [NSString stringWithFormat:@"%u", arc4random_uniform(13)];
        NSString *day = [NSString stringWithFormat:@"%u", arc4random_uniform(29)];
        [dic1 setValue:month forKey:@"month"];
        [dic1 setValue:day forKey:@"day"];
        [dic1 setValue:@[@"Click to add an event on this day"] forKey:@"event"];
        [self.sourcyAry addObject:dic1];
    }
}

-(void)dataList {
    NSArray *array = [YXNetWorkCache httpCacheForURL:Festival parameters:nil];
    if (!array.count) {
        self.sourcyAry = [NSMutableArray array];
        [self addEvent];
        [YXNetWorkCache setHttpCache:self.sourcyAry URL:Festival parameters:nil];
    } else {
        self.sourcyAry = [NSMutableArray arrayWithArray:array];
    }
    [self.tableView reloadData];
}


- (void)makeView {
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).offset(UIEdgeInsetsMake(0, 0, TABBAR_HEIGHT, 0));
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sourcyAry.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *array = self.sourcyAry[section][@"event"];
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeCell"];
    if (!cell) {
        cell = [[HomeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HomeCell"];
    }
    NSDictionary *dataDic = self.sourcyAry[indexPath.section];
    NSArray *arrayr = dataDic[@"event"];
    [cell showCell:arrayr[indexPath.row]];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    headerView.backgroundColor = [UIColor whiteColor];
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    lineView.backgroundColor = DefaultBackColor;
    [headerView addSubview:lineView];
    UILabel *headtitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH, 20)];
    headtitle.textColor = TextBlackColor;
    headtitle.font = RegularMiddelFont;
    NSDictionary *dataDic = self.sourcyAry[section];
    headtitle.text = [NSString stringWithFormat:@"%@-%@",dataDic[@"month"], dataDic[@"day"]];
    [headerView addSubview:headtitle];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        NSLog(@"add");
        AddFestivalEventController *vc = [AddFestivalEventController new];
        vc.dataArray = self.sourcyAry;
        vc.dataDic = self.sourcyAry[indexPath.section];
        vc.index = indexPath.section;
        vc.isShowInfo = NO;
        [vc setReloadTableViewBlcok:^{
            NSArray *array = [YXNetWorkCache httpCacheForURL:Festival parameters:nil];
            self.sourcyAry = [NSMutableArray arrayWithArray:array];
            [self.tableView reloadData];
        }];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else
    {
        NSDictionary *dic = self.sourcyAry[indexPath.section];
        NSArray *array = dic[@"event"];
        AddFestivalEventController *vc = [AddFestivalEventController new];
        vc.isShowInfo = YES;
        vc.dataDic = self.sourcyAry[indexPath.section];
        vc.infoStr = array[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    }

}

- (void)makeHeadView {
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    self.tableView.tableHeaderView = headView;
    
    [headView addSubview:self.waveView];
    [self addWave];
    [headView addSubview:self.maskLabel];
}

- (WaveView *)waveView {
    if (!_waveView) {
        _waveView = [[WaveView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 150) / 2.0, 25, 150, 150)];
        _waveView.layer.cornerRadius = 75;
        _waveView.layer.masksToBounds = YES;
        _waveView.backgroundColor = [UIColor clearColor];
    }
    return _waveView;
}

- (MaskLabel *)maskLabel {
    if (!_maskLabel) {
        _maskLabel = [[MaskLabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 150) / 2.0, 25, 150, 150)];
        _maskLabel.font = [UIFont systemFontOfSize:64];
        _maskLabel.textAlignment = NSTextAlignmentCenter;
        _maskLabel.layer.cornerRadius = 75;
        _maskLabel.layer.masksToBounds = YES;
        _maskLabel.backgroundColor = HEX_COLOR(0xF7CE47);
        _maskLabel.text = [NSString stringWithFormat:@"Plan"];
        _maskLabel.maskTextEnabled = YES;
    }
    return _maskLabel;
}

- (void)addWave {
    Wave *wave = [Wave defaultWave];
    wave.offsetX = 0;
    wave.offsetY = 75;
    wave.height  = 20;
    wave.width   = 200;
    wave.speedX  = 3;
    wave.fillColor = [UIColor colorWithRed:0.000 green:0.502 blue:1.000 alpha:0.45].CGColor;
    wave.backgroundColor = [UIColor redColor].CGColor;
    [self.waveView addWave:wave];
    [self.waveView startWaveAnimate];
}

@end
